/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package logica;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;

/**
 *
 * @author francisco tiapa
 */
public class ListaMaquinaXML {
    
     private List<MaquinaXML> laLista_m;
     private Connection conexion;

    public ListaMaquinaXML() {
       laLista_m = new ArrayList<MaquinaXML>();
    }

    public boolean AgregarMaquina(MaquinaXML laMaquina)
    {
     return laLista_m.add(laMaquina);
    }

    public List<MaquinaXML> getLaLista_m() {
        return laLista_m;
    }

    public void setLaLista_m(List<MaquinaXML> laLista_m) {
        this.laLista_m = laLista_m;
    }
    public void updatemaquinaBd () throws SQLException
    {
 
        ConexionBD c = new ConexionBD();
        conexion = c.conectarBD();
        int id_maquina =0;
        ///// busco el id de la maquina
        String query = "SELECT id FROM maquinas ";
        PreparedStatement ps = conexion.prepareStatement(query);
        ResultSet rs = ps.executeQuery();
        while(rs.next()){
           id_maquina= rs.getInt(1);
        }
        // vacio la informacion de la tabla
        String query1 = "DELETE FROM maquinas";
        PreparedStatement ps2 = conexion.prepareStatement(query1);
        int rs2 = ps2.executeUpdate();

        ////// actualizo la infomracion de la maquina
        Iterator<MaquinaXML> elIterator = laLista_m.iterator();
     while(elIterator.hasNext()==true)
      {
      MaquinaXML laMaquina=elIterator.next();

         if( laMaquina.getId()==id_maquina ){
       String query2 = "insert into maquinas values(?,?,?,?,?,?,?,?)";
       PreparedStatement ps3 = conexion.prepareStatement(query2);
       ps3.setInt(1,laMaquina.getId());
       ps3.setString(2,laMaquina.getLatitud());
       ps3.setString(3,laMaquina.getLogitud());
       ps3.setString(4,laMaquina.getNombre());
       ps3.setString(5, laMaquina.getDescripcion());
       ps3.setString(6, laMaquina.getStatus());
       ps3.setDate(7,null);
       ps3 .setDate(8, null);
       int rs3 = ps3.executeUpdate();
          }
      }
        conexion.close();
 }
    }

